//
//  CallNotesTranslatorsEvaluateView.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/3/17.
//  Copyright © 2025 考拉🐨. All rights reserved.
//

import UIKit
import Reusable

extension CallNotesTranslatorsEvaluateView: NibLoadable {}

class CallNotesTranslatorsEvaluateView: UIView {
    
    @IBOutlet weak var markView: UIView!
    @IBOutlet weak var ratingView: RatingStar!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionLayoutHeight: NSLayoutConstraint!
    
    @IBOutlet weak var titleLabel0: UILabel!
    @IBOutlet weak var titleLabel1: UILabel!
    
    var model: CallNotesTranslatorModel? {
        didSet {
            guard let model = model else { return }
            
            ratingView.rating = CGFloat(model.rating)
            list = model.ratingList
        }
    }
    var list: [BaseUIModel] = [] {
        didSet {
            updateCollectionView()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        markView.addRoundedCorners(radius: 8)
        backgroundColor = UIColor(hex: "F7F7F7")
        
        titleLabel0.text = "服务整体评级".localized(.call)
        titleLabel1.text = "评价".localized(.call)
        
        ratingView.isEnable = true
        ratingView.style = .full
        ratingView.ratingMin = 1
        ratingView.space = 8
        ratingView.delegate = self
        ratingView.backgroundColor = .clear
        ratingView.imageLight = UIImage(named: "svg_wujiaoxing")!.withTintColor(UIColor(hex: "#FFE05F"))
        ratingView.imageDark = UIImage(named: "svg_wujiaoxing")!.withTintColor(UIColor(hex: "#DADADA"))
        
        
        let layout = YLCollectionViewFlowLayout()
        layout.delegate = self
        layout.columnSpacing = 8
        layout.rowSpacing = 8
        
        collectionView.setCollectionViewLayout(layout, animated: true)
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.isScrollEnabled = false
        collectionView.bounces = false
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(cellType: CallNotesTranslationEvaluateItemCell.self)
    }
    
    func updateCollectionView() {
        collectionView.reloadData()
        collectionView.layoutIfNeeded()
        collectionLayoutHeight.constant = collectionView.contentSize.height
        
    }
}

extension CallNotesTranslatorsEvaluateView: RatingStarDelegate {
    func ratingDidChange(_ view: RatingStar, rating: CGFloat) {
        guard let model = model else { return }
        
        switch rating {
        case 0, 1:
            if model.rating == 0 || model.rating == 1 {
                return
            }
        case 2, 3:
            if model.rating == 2 || model.rating == 2 {
                return
            }
            
        case 4, 5:
            if model.rating == 4 || model.rating == 5 {
                return
            }
        default:
            break
        }
        
        model.rating = Int(rating)
        model.ratingList = CallNotesTranslatorModel.evaluateData(rating: model.rating)
        list = model.ratingList
        
    }
    
    
}


extension CallNotesTranslatorsEvaluateView: UICollectionViewDelegate, UICollectionViewDataSource, YLCollectionFlowLayoutDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        list.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: CallNotesTranslationEvaluateItemCell.self)
        let model = list[indexPath.row]
        cell.titleLabel.text = model.title.localized(.call)
        cell.iconImageView.image = model.imageName.toImage
        cell.contentView.backgroundColor = model.isSelected ? UIColor.themeColor: UIColor(hex: "F3f3f6")
        cell.titleLabel.textColor = model.isSelected ? UIColor.white: UIColor(hex: "#666666")
        cell.addRoundedCorners(radius: 16)
        return cell
    }
    
    func collectionView(sizeForItemAt index: Int) -> CGSize {
        let model = list[index]
        var width = model.title.localized(.call).sg.size(width: CGFLOAT_MAX, font: .systemFont(ofSize: 12)).width
        
        width += CGFloat(10*2+20)
        width = ceil(width)
        width = min(xScreenW - 24*2, width)
        return CGSizeMake(width, 32)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let model = list[indexPath.row]
        model.isSelected = !model.isSelected
        collectionView.reloadData()
    }
}

